package com.gonsin.easycount;

import com.gonsin.easycount.bus.EasyBus;
import com.gonsin.easycount.bus.OptionListener;
import com.gonsin.easycount.exception.EasyCountException;
import com.gonsin.easycount.config.EasyCountBuilder;
import com.gonsin.easycount.model.UserInfoOption;
import com.gonsin.easycount.service.ICountService;
import com.gonsin.easycount.service.impl.CountServiceImpl;

import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 对外提供的唯一方法
 */
public class EasyCount {

    protected static ICountService _instance;
    /**
     * 初始化EasyCount用到的所有组件，该方法应该在程序启动时执行
     */
    public static EasyCountBuilder buildEasyCount() {

        return new EasyCountBuilder();
    }

    /**
     * 初始化数据环境
     */
    public static void init(String topic, List<UserInfoOption> userInfoOptions, Object holder){
        _instance.init(topic, userInfoOptions, holder);
    }


    /**
     * @deprecated 此方法没什么用
     * 是否已初始化数据环境
     */
    @Deprecated
    public static boolean isInit(String topic){
        return true;
    }

    /**
     * 释放数据环境
     */
    public static void release(String topic){
        _instance.release(topic);
    }


    public static <T> T getHolder(String topic){
        return _instance.getHolder(topic);
    }


    /**
     * 进行加一操作
     */
    public static void increase(String topic, String userInfo, String  option){
        _instance.increase(topic, userInfo, new String[]{option}, null);
    }


    /**
     * 进行加一操作
     */
    public static void increase(String topic, String userInfo, String[]  option){
        _instance.increase(topic, userInfo, option, null);
    }


    /**
     * 进行加一操作，并附带信息
     */
    public static void increase(String topic, String userInfo, String[] option, Object extraData){
        _instance.increase(topic, userInfo, option, extraData);
    }

    /**
     * 进行加一操作，并附带信息
     */
    public static void increase(String topic, String userInfo, String option, Object extraData){
        _instance.increase(topic, userInfo, new String[]{option}, extraData);
    }

    /**
     * 获取总数
     */
    public static long sum(String topic){
        return _instance.sum(topic);
    }

    /**
     * 强制立即输出一次回调
     */
    public static void flush(String topic){
        _instance.flush(topic);
    }

    /**
     * 尝试触发回调
     */
    public static void testFlush() {
        _instance.testFlush();
    }


    /**
     * 获取选项总数
     */
    public static long sumOption(String topic, String option){
        return _instance.sumOptions(topic, option);
    }

    /**
     * 返回某人员的票数
     */
    public static long sumUserInfo(String topic, String userInfo){
        return _instance.sumUserInfos(topic, userInfo);
    }

    /**
     * 获取人员自己的选项
     */
    public static String[] options(String topic, String userInfo){
        return _instance.options(topic, userInfo);
    }

    /**
     * 获取某选项的人员
     */
    public static Set<String> userInfos(String topic, String option){
        return _instance.userInfos(topic, option);
    }


    /**
     * 所有人的选项详情
     */
    public static Set<UserInfoOption> userInfoOptions(String topic) {
        return _instance.userInfoOptions(topic);
    }

    /**
     * 注册选项变更消息
     */
    public static void registerOptionListener(String topic, OptionListener listener){
        EasyBus.getInstance().registerOptionListener(topic, listener);
    }


    /**
     * 注册选项变更消息
     */
    public static void unregisterOptionListener(String topic){
        EasyBus.getInstance().unregisterOptionListener(topic);
    }


    /**
     * 设置单例
     */
    public static void setInstance(CountServiceImpl countService) {
        _instance = countService;
    }
}
